/*
 * MainFrame.java
 *
 * Created on Nov 20, 2009, 8:56:31 AM
 *
 * Description:
 *      Cutthroat Caverns Toolbox
 *
 * Current Modules:
 *      Initiative Dealer and tracker
 *      Player death tracker
 *
 * Future Modules:
 *      Player health and prestige tracking
 *      Monster database and health tracking
 *
 */

package sequence;

import java.util.HashMap;
import java.util.Stack;

/**
 *
 * @author dkimnach
 */
public class Stepper {

    protected Stack<String> history;
    protected HashMap<String, Step> game = new HashMap<String, Step>();
    protected Step current;
    protected Boolean monsterDead = false;
    protected Boolean allDead = false;


    protected String begin = "begin";
    protected String end = "end";
    protected String enc = "encounter";
    protected String pot = "potions";
    protected String encInit = "enc_init";
    protected String disDraw = "dis_draw";
    protected String pres = "prestige";
    protected String encBead = "enc_bead";
    protected String set = "set";
    protected String play = "play";
    protected String dmgBead = "dmg_bead";
    protected String monA = "mon_attacks";
    protected String rInit = "round_init";
    protected String initA = "init_attacks";
    protected String drawOne = "draw_one";
    
    public Stepper() {
        game.put(begin,     new Step(begin,     enc,        "New Game Setup"));
        initEncounter();    // setup encounter
        initRound();        // encounter attack rounds
        initReward();       // end of encounter
        game.put(end,       new Step(end,       begin,      "End of Game"));

    }

    private void initEncounter() {
        // encounter loop
        game.put(enc,       new Step(enc,       pot,        "New Encounter"));
        game.put(pot,       new Step(pot,       encInit,    "Tap Potions"));
        game.put(encInit,   new Step(encInit,   set,        "Deal First Initiatives"));
        game.put(disDraw,   new Step(disDraw,   enc,        "Discard & Draw (init order)"));
    }

    private void initReward() {
        game.put(pres,      new Step(pres,      encBead,    "Award Prestige"));
        game.put(encBead,   new Step(encBead,   disDraw,    "Move Encounter Bead"));
    }

    private void initRound() {
        game.put(set,       new Step(set,       play,       "Set Attacks"));
        game.put(play,      new Step(play,      dmgBead,    "Plat Attacks (init order)"));
        game.put(dmgBead,   new Step(dmgBead,   monA,       "Damage Monster"));
        game.put(monA,      new Step(monA,      rInit,      "Damage-based Monster Attacks"));
        game.put(rInit,     new Step(rInit,     initA,      "Deal Initiatives"));
        game.put(initA,     new Step(initA,     drawOne,    "Initiative-based Monster Attacks"));
        game.put(drawOne,   new Step(drawOne,   set,        "Draw One (init order)"));
    }

    public Step next() throws Exception {
        Step s;

        history.push(current.name); // save current
        s = goTo(current.next());   // go to next
        current = s;                // new current

        return s;
    }

    public Step last() throws Exception {
        Step s;

        s = goTo(history.pop());
        
        return s;
    }

    public Step goTo(String step) throws Exception {
        Step s;

        if (game.containsKey(step)) {
            s = game.get(step);
        } else {
            throw new Exception("ERROR: step not found. cannot go to step '" + step + "'");
        }

        return s;
    }
}
